<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * Profile controller
 * @author Simon, Denis, Dennis
 *
 */
class Profile extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		require_once('user.php');
		$this->user=new User();
	}

	/**
	 * Index Page for this controller.
	 *
	 */
	public function index()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);
		if($loggedIn==true) {
			$this->load->view('profile');
		} else {
			$this->load->view('503');
		}
		$this->load->view('footer');
	}

	/**
	 * method to edit the profile data from one user
	 * loads the user by session ID
	 */
	public function editProfileData()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);


		$data['user'] = $this->user_model->getUserByID($_SESSION['id']);

		$this->load->view('profile_personal_data', $data);
		$this->load->view('footer');
	}

	/**
	 * method to edit the login data from one user. password and email
	 */
	public function editLoginData()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);

		$data['user'] = $this->user_model->getUserByID($_SESSION['id']);

		$this->load->view('profile_login_data', $data);
		$this->load->view('footer');
	}

	/**
	 * method to store the new login data of a user to the database
	 */
	public function saveLoginData()
	{
		$userdata = $this->user_model->getUserByID($_SESSION['id']);
		$userdata->password =  sha1( md5(trim($_POST['newPassword'])));
		$userdata->email = trim($_POST['email']);

		if( $this->user->login($_SESSION['username'], $_POST['oldPassword']))
		{

		} else {
			echo "Invalid old password";
			return false;
		}

		if(empty($userdata->email))
		{
			echo "no mail";
			return false;
		}
		if(strnatcmp ( $_POST['newPasswordRpt'] , $_POST['newPassword'])!=0){
			echo "repeated password does not match";
			return false;
		}


		$error = $this->user_model->modifyUser($_SESSION['id'], $userdata);
		if($error != 1){
			echo $error;
			return;
		}


		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);
		$this->load->view('profile');
		$this->load->view('footer');

		$data['redirect_message'] = "Password successfully changed";
		$this->load->view('redirect', $data);
		return;
	}

	/**
	 * method to save the profile data of a user to the database
	 */
	public function saveProfileData()
	{
		$userdata = $this->user_model->getUserByID($_SESSION['id']);
		$surname = trim($_POST['surname']);
		$name = trim($_POST['name']);
		$pro = trim($_POST['pro']);
		$size = trim($_POST['size']);
		$weight = trim($_POST['weight']);
		$gender = trim($_POST['gender']);
		$city = trim($_POST['city']);
		$country = trim($_POST['country']);
		$birthday = trim($_POST['birthday']);

		$error = $this->user_model->modifyUser($_SESSION['id'], $surname, $name, $pro, $size, $weight, $gender, $city, $country, $birthday);
		if($error != 1){
			return $error;
		}

		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);
		$this->load->view('profile');
		$this->load->view('footer');
	}

	/**
	 * method to load the equipment data from one user by username
	 */
	public function equipment()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}



		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);
		$this->load->view('profile_equipment');
		$this->load->view('footer');
	}

	/**
	 * method to add new equipment to one user.
	 */
	public function addEquipment()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);

		$data['shoe'] = $this->shoe_model->getAllBrooksShoes();

		$this->load->view('profile_equipment_add', $data);
		$this->load->view('footer');
	}

	/**
	 * method to add new equipment to one user and store it to the database
	 */
	public function saveAddEquipment()
	{
		$shoeID = trim($_POST['shoeID']);
		$isCustom = trim($_POST['isCustom']);

		if($isCustom == "on"){
			$modell = trim($_POST['modell']);
			$manufactor = trim($_POST['manufactor']);
			$runeffort = trim($_POST['runeffort']);

			$error = $this->shoe_model->addShoe($modell,$manufactor,$runeffort, 1);
			if($error != 1){
				echo $error;
				return;
			}

			$shoe = $this->shoe_model->getShoeByModell($modell);
			$shoeID = $shoe->ID;
		}


		$error = $this->shoeUser_model->addUserShoe($_SESSION['id'], $shoeID);
		if($error != 1){
			echo $error;
			return;
		}
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);

		$data['shoe'] = $this->shoe_model->getAllShoes();

		$this->load->view('profile_equipment_add', $data);
		$this->load->view('footer');
	}

	/**
	 * method to edit equipment from one user and store it to the database
	 */
	public function saveEditEquipment()
	{
		$shoeID = trim($_POST['shoeID']);
		$modell = trim($_POST['modell']);
		$manufactor = trim($_POST['manufactor']);
		$runeffort = trim($_POST['runeffort']);

		$error = $this->shoe_model-> updateShoe($shoeID, $modell,$manufactor,$runeffort);
		if($error != 1){
			echo $error;
			return;
		}
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);


		$this->load->view('profile_equipment');
		$this->load->view('footer');
	}

	/**
	 * method to edit existing equipment from one user
	 */
	public function editEquipment()
	{

		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);


		$data['shoes'] = $this->shoe_model->getAllCustomShoes();

		$this->load->view('profile_equipment_edit', $data);
		$this->load->view('footer');
	}

	/**
	 * method to permanently delete one user account
	 */
	public function deleteAccount()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'profile';
		$this->load->view('menu',$menu);
		$this->load->view('profile_delete_account');
		$this->load->view('footer');
	}
}